<?php

namespace app\admin\controller;

use app\models\Admin;
use think\Db;
use think\Facade\Config as Configs;
use app\models\{Order, Orderlog, Region};


class Main extends Base
{
    public function alldatas()
    {
        $data = [];
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $getdata = input('param.');
        $time = !empty($getdata['starttime']) ? $getdata['starttime'] . ' 00:00:00' : date('Y-m-d') . ' 00:00:00';
        $endtime = !empty($getdata['endtime']) ? $getdata['endtime'] . ' 23:59:59' : date('Y-m-d') . ' 23:59:59';
        //获取全国省份
        $returnArr = [];
        if ($admin['type'] == 4 || $admin['type'] == 3) {
            $getdata['street'] = $admin['street'];
        }
        if ($admin['type'] == 2) {
            $getdata['area'] = $admin['area'];
            if (!empty($getdata['street']) && !in_array($getdata['area'], $admin['area'])) {
                $getdata['street'] = 0;
            }
        }
        if (!empty($getdata['street'])) {
            $list = (new Region())->where(['id' => $getdata['street'], 'level' => 4])->select();
            foreach ($list as $lists) {
                $orderlist = (new Order())
                    ->whereBetweenTime('ordertime', $time, $endtime)
                    ->where(['status' => 3])->where(['street' => $lists['id']])->select();
                $returnArr[] = [
                    'name' => $lists['name'],
                    'counts' => count($orderlist),
                    'total' => round(array_sum(array_column($orderlist->toArray(), 'total')), 2)
                ];
            }
        } elseif (!empty($getdata['area']) && $admin['type'] <= 3) {
            $id_area = (new Region())->where(['area_code' => $getdata['area'], 'level' => 3])->value('id');
            $list = (new Region())->where(['parent_id' => $id_area, 'level' => 4])->select();
            foreach ($list as $lists) {
                $orderlist = (new Order())
                    ->whereBetweenTime('ordertime', $time, $endtime)
                    ->where(['status' => 3])->where(['street' => $lists['id']])->select();
                $returnArr[] = [
                    'name' => $lists['name'],
                    'counts' => count($orderlist),
                    'total' => round(array_sum(array_column($orderlist->toArray(), 'total')), 2)
                ];
            }
        } elseif (!empty($getdata['city']) && $admin['type'] <= 1) {
            $id_area = (new Region())->where(['area_code' => $getdata['city'], 'level' => 2])->value('id');
            $list = (new Region())->where(['parent_id' => $id_area, 'level' => 3])->select();
            foreach ($list as $lists) {
                $orderlist = (new Order())
                    ->whereBetweenTime('ordertime', $time, $endtime)
                    ->where(['status' => 3])->where(['area' => $lists['area_code']])->select();
                $returnArr[] = [
                    'name' => $lists['name'],
                    'counts' => count($orderlist),
                    'total' => round(array_sum(array_column($orderlist->toArray(), 'total')), 2)
                ];
            }
        } elseif (!empty($getdata['province']) && $admin['type'] <= 1) {
            $id_area = (new Region())->where(['area_code' => $getdata['province'], 'level' => 1])->value('id');
            $list = (new Region())->where(['parent_id' => $id_area, 'level' => 2])->select();
            foreach ($list as $lists) {
                $orderlist = (new Order())
                    ->whereBetweenTime('ordertime', $time, $endtime)
                    ->where(['status' => 3])->where(['city' => $lists['area_code']])->select();
                $returnArr[] = [
                    'name' => $lists['name'],
                    'counts' => count($orderlist),
                    'total' => round(array_sum(array_column($orderlist->toArray(), 'total')), 2)
                ];
            }
        } elseif ($admin['type'] <= 1) {
            $list = (new Region())->where(['parent_id' => 0, 'level' => 1])->select();
            foreach ($list as $lists) {
                $orderlist = (new Order())
                    ->whereBetweenTime('ordertime', $time, $endtime)
                    ->where(['status' => 3])->where(['province' => $lists['area_code']])->select();
                $returnArr[] = [
                    'name' => $lists['name'],
                    'counts' => count($orderlist),
                    'total' => round(array_sum(array_column($orderlist->toArray(), 'total')), 2)
                ];
            }
        }
        if ($this->request->isPost()) {
            return json(['status' => 0, 'msg' => '操作成功', 'data' =>  $returnArr]);
        } else {
            return view(null, ['returnArr' => $returnArr]);
        }
    }

    public function welcome1()
    {
        ini_set('memory_limit', '1256M');
        $data = [];
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $datas = $this->request->get();
        $time = !empty($datas['starttime']) ? $datas['starttime'] . ' 00:00:00' : date('Y-m-d') . ' 00:00:00';
        $endtime = !empty($datas['endtime']) ? $datas['endtime'] . ' 24:00:00' : date('Y-m-d') . ' 24:00:00';
        $data['order'] = 0;
        $data['todayorder'] = 0;
        $data['total'] = 0;
        $data['weight'] = 0;
        $data['todaytotal'] = 0;
        $data['todayweight'] = 0;
        $data['result'] = [];
        //所有订单
        if ($admin['type'] == 1) {
            if (!empty($datas['street'])) {
                $getdata['street'][] = $datas['street'] ?? 0;
            } elseif (!empty($datas['area'])) {
                $getdata['area'][] = $datas['area'] ?? 0;
            } elseif (!empty($datas['city'])) {
                $getdata['city'][] = $datas['city'] ?? 0;
            } elseif (!empty($datas['province'])) {
                $getdata['province'][] = $datas['province'] ?? 0;
            }
            $order = (new Order())->where('status', 3)->select();
        } elseif ($admin['type'] == 2) {
            $getdata['area'] = $admin['area'];
            $order = (new Order())->where('status', 3)->where(['status' => 3, 'area' => $admin['area']])->select();
        } elseif ($admin['type'] == 3 || $admin['type'] == 4) {
            $getdata['street'] = $admin['street'];
            $order = (new Order())->where('status', 3)->where(['status' => 3, 'street' => $admin['street']])->select();
        } else {
            $order = [];
        }
        if (!empty($getdata['street'])) {
            foreach ($getdata['street'] as $street) {
                $data['result'][$street]['name'] = (new Region())->where(['id' => $street, 'level' => 4])->value('name') ?? '';
                $data['result'][$street]['total'] = 0;
                $data['result'][$street]['weight'] = 0;
                $data['result'][$street]['order'] = 0;
                $data['result'][$street]['todaytotal'] = 0;
                $data['result'][$street]['todayweight'] = 0;
                $data['result'][$street]['todayorder'] = 0;
            }
        } elseif (!empty($getdata['area'])) {
            foreach ($getdata['area'] as $area) {
                $data['result'][$area]['name'] = (new Region())->where(['area_code' => $area, 'level' => 3])->value('name') ?? '';
                $data['result'][$area]['total'] = 0;
                $data['result'][$area]['weight'] = 0;
                $data['result'][$area]['order'] = 0;
                $data['result'][$area]['todaytotal'] = 0;
                $data['result'][$area]['todayweight'] = 0;
                $data['result'][$area]['todayorder'] = 0;
            }
        } elseif (!empty($getdata['city'])) {
            foreach ($getdata['city'] as $city) {
                $data['result'][$city]['name'] = (new Region())->where(['area_code' => $city, 'level' => 2])->value('name') ?? '';
                $data['result'][$city]['total'] = 0;
                $data['result'][$city]['weight'] = 0;
                $data['result'][$city]['order'] = 0;
                $data['result'][$city]['todaytotal'] = 0;
                $data['result'][$city]['todayweight'] = 0;
                $data['result'][$city]['todayorder'] = 0;
            }
        } elseif (!empty($getdata['province'])) {
            foreach ($getdata['province'] as $province) {
                $data['result'][$province]['name'] = (new Region())->where(['area_code' => $province, 'level' => 1])->value('name') ?? '';
                $data['result'][$province]['total'] = 0;
                $data['result'][$province]['weight'] = 0;
                $data['result'][$province]['order'] = 0;
                $data['result'][$province]['todaytotal'] = 0;
                $data['result'][$province]['todayweight'] = 0;
                $data['result'][$province]['todayorder'] = 0;
            }
        }

        foreach ($order as $orderlogs) {
            $data['total'] += $orderlogs['total'];
            $data['weight'] += $orderlogs['weight'];
            if ($orderlogs['ordertime'] >= $time && $orderlogs['ordertime'] < $endtime) {
                $data['todaytotal'] += $orderlogs['total'];
                $data['todayweight'] += $orderlogs['weight'];
            }
            if (!empty($getdata['street']) && in_array($orderlogs['street'], $getdata['street'])) {
                if (empty($data['result'][$orderlogs['street']]['name'])) {
                    $data['result'][$orderlogs['street']]['name'] = (new Region())->where(['id' =>  $orderlogs['street'], 'level' => 4])->value('name') ?? '';
                }
                $data['result'][$orderlogs['street']]['total'] = ($data['result'][$orderlogs['street']]['total'] ?? 0) +  $orderlogs['total'];
                $data['result'][$orderlogs['street']]['weight'] = ($data['result'][$orderlogs['street']]['weight'] ?? 0) +  $orderlogs['weight'];
                $data['result'][$orderlogs['street']]['order'] = ($data['result'][$orderlogs['street']]['order'] ?? 0) +  1;
                if ($orderlogs['ordertime'] >= $time && $orderlogs['ordertime'] < $endtime) {
                    $data['result'][$orderlogs['street']]['todaytotal'] = ($data['result'][$orderlogs['street']]['todaytotal'] ?? 0) +  $orderlogs['total'];
                    $data['result'][$orderlogs['street']]['todayweight'] = ($data['result'][$orderlogs['street']]['todayweight'] ?? 0) +  $orderlogs['weight'];
                    $data['result'][$orderlogs['street']]['todayorder'] = ($data['result'][$orderlogs['street']]['todayorder'] ?? 0) +  1;
                }
            } elseif (!empty($getdata['area']) && in_array($orderlogs['area'], $getdata['area'])) {
                if (empty($data['result'][$orderlogs['area']]['name'])) {
                    $data['result'][$orderlogs['area']]['name'] = (new Region())->where(['area_code' =>  $orderlogs['area'], 'level' => 3])->value('name') ?? '';
                }
                $data['result'][$orderlogs['area']]['total'] = ($data['result'][$orderlogs['area']]['total'] ?? 0) +  $orderlogs['total'];
                $data['result'][$orderlogs['area']]['weight'] = ($data['result'][$orderlogs['area']]['weight'] ?? 0) +  $orderlogs['weight'];
                $data['result'][$orderlogs['area']]['order'] = ($data['result'][$orderlogs['area']]['order'] ?? 0) +  1;
                if ($orderlogs['ordertime'] >= $time && $orderlogs['ordertime'] < $endtime) {
                    $data['result'][$orderlogs['area']]['todaytotal'] = ($data['result'][$orderlogs['area']]['todaytotal'] ?? 0) +  $orderlogs['total'];
                    $data['result'][$orderlogs['area']]['todayweight'] = ($data['result'][$orderlogs['area']]['todayweight'] ?? 0) +  $orderlogs['weight'];
                    $data['result'][$orderlogs['area']]['todayorder'] = ($data['result'][$orderlogs['area']]['todayorder'] ?? 0) +  1;
                }
            } elseif (!empty($getdata['city'])) {
                if (empty($data['result'][$orderlogs['city']]['name'])) {
                    $data['result'][$orderlogs['city']]['name'] = (new Region())->where(['area_code' =>  $orderlogs['city'], 'level' => 3])->value('name') ?? '';
                }
                $data['result'][$orderlogs['city']]['total'] = ($data['result'][$orderlogs['city']]['total'] ?? 0) +  $orderlogs['total'];
                $data['result'][$orderlogs['city']]['weight'] = ($data['result'][$orderlogs['city']]['weight'] ?? 0) +  $orderlogs['weight'];
                $data['result'][$orderlogs['city']]['order'] = ($data['result'][$orderlogs['city']]['order'] ?? 0) +  1;
                if ($orderlogs['ordertime'] >= $time && $orderlogs['ordertime'] < $endtime) {
                    $data['result'][$orderlogs['city']]['todaytotal'] = ($data['result'][$orderlogs['city']]['todaytotal'] ?? 0) +  $orderlogs['total'];
                    $data['result'][$orderlogs['city']]['todayweight'] = ($data['result'][$orderlogs['city']]['todayweight'] ?? 0) +  $orderlogs['weight'];
                    $data['result'][$orderlogs['city']]['todayorder'] = ($data['result'][$orderlogs['city']]['todayorder'] ?? 0) +  1;
                }
            } elseif (!empty($getdata['province'])) {
                if (empty($data['result'][$orderlogs['province']]['name'])) {
                    $data['result'][$orderlogs['province']]['name'] = (new Region())->where(['area_code' =>  $orderlogs['province'], 'level' => 3])->value('name') ?? '';
                }
                $data['result'][$orderlogs['province']]['total'] = ($data['result'][$orderlogs['province']]['total'] ?? 0) +  $orderlogs['total'];
                $data['result'][$orderlogs['province']]['weight'] = ($data['result'][$orderlogs['province']]['weight'] ?? 0) +  $orderlogs['weight'];
                $data['result'][$orderlogs['province']]['order'] = ($data['result'][$orderlogs['province']]['order'] ?? 0) +  1;
                if ($orderlogs['ordertime'] >= $time && $orderlogs['ordertime'] < $endtime) {
                    $data['result'][$orderlogs['province']]['todaytotal'] = ($data['result'][$orderlogs['province']]['todaytotal'] ?? 0) +  $orderlogs['total'];
                    $data['result'][$orderlogs['province']]['todayweight'] = ($data['result'][$orderlogs['province']]['todayweight'] ?? 0) +  $orderlogs['weight'];
                    $data['result'][$orderlogs['province']]['todayorder'] = ($data['result'][$orderlogs['province']]['todayorder'] ?? 0) +  1;
                }
            }
        }
        return view(null, [
            'admin' => $admin,
            'data' => $data,
            'time' => $time . '~' . $endtime
        ]);
    }

    public function welcome()
    {
        ini_set('memory_limit', '1256M');
        $data = [];
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $getdata = $this->request->get();
        $time = !empty($getdata['starttime']) ? $getdata['starttime'] . ' 00:00:00' : date('Y-m-d') . ' 00:00:00';
        $endtime = !empty($getdata['endtime']) ? $getdata['endtime'] . ' 00:00:00' : date('Y-m-d') . ' 00:00:00';
        $data['result'] = [];
        $orderlog = (new Orderlog())->select();
        if ($admin['type'] == 1) {
            $order = (new Order())->where(['status' => 3])->column('ordersn');
            $todayorder = (new Order())->where(['status' => 3])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
            if (!empty($getdata['street'])) {
                $streets = (new Region())->where(['id' => $getdata['street'], 'level' => 4])->column('id');
                foreach ($streets as $k =>  $street) {
                    $data['result'][$k]['total'] = 0;
                    $data['result'][$k]['weight'] = 0;
                    $data['result'][$k]['todaytotal'] = 0;
                    $data['result'][$k]['todayweight'] = 0;
                    $orderson =  (new Order())->where(['status' => 3, 'street' => $street])->column('ordersn');
                    $todayorderson =  (new Order())->where(['status' => 3, 'street' => $street])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                    $data['result'][$k]['name'] = (new Region())->where(['id' => $street])->value('name');
                    $data['result'][$k]['order'] = count($orderson);
                    $data['result'][$k]['todayorder'] = count($todayorderson);
                    foreach ($orderlog as $orderlogs) {
                        if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                            $data['result'][$k]['total'] += $orderlogs['total'];
                            $data['result'][$k]['weight'] += $orderlogs['weight'];
                        }
                        if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                            $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                            $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                        }
                    }
                }
            } elseif (!empty($getdata['area'])) {
                $areas = (new Region())->where(['area_code' => $getdata['area'], 'level' => 3])->column('area_code');
                foreach ($areas as $k =>  $area) {
                    $data['result'][$k]['total'] = 0;
                    $data['result'][$k]['weight'] = 0;
                    $data['result'][$k]['todaytotal'] = 0;
                    $data['result'][$k]['todayweight'] = 0;
                    $orderson =  (new Order())->where(['status' => 3, 'area' => $area])->column('ordersn');
                    $todayorderson =  (new Order())->where(['status' => 3, 'area' => $area])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                    $data['result'][$k]['name'] = (new Region())->where(['area_code' => $area, 'level' => 3])->value('name');
                    $data['result'][$k]['order'] = count($orderson);
                    $data['result'][$k]['todayorder'] = count($todayorderson);
                    foreach ($orderlog as $orderlogs) {
                        if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                            $data['result'][$k]['total'] += $orderlogs['total'];
                            $data['result'][$k]['weight'] += $orderlogs['weight'];
                        }
                        if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                            $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                            $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                        }
                    }
                }
            } elseif (!empty($getdata['city'])) {
                $citys = (new Region())->where(['area_code' => $getdata['city'], 'level' => 2])->column('area_code');
                foreach ($citys as $k =>  $city) {
                    $data['result'][$k]['total'] = 0;
                    $data['result'][$k]['weight'] = 0;
                    $data['result'][$k]['todaytotal'] = 0;
                    $data['result'][$k]['todayweight'] = 0;
                    $orderson =  (new Order())->where(['status' => 3, 'city' => $city])->column('ordersn');
                    $todayorderson =  (new Order())->where(['status' => 3, 'city' => $city])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                    $data['result'][$k]['name'] = (new Region())->where(['area_code' => $city])->value('name');
                    $data['result'][$k]['order'] = count($orderson);
                    $data['result'][$k]['todayorder'] = count($todayorderson);
                    foreach ($orderlog as $orderlogs) {
                        if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                            $data['result'][$k]['total'] += $orderlogs['total'];
                            $data['result'][$k]['weight'] += $orderlogs['weight'];
                        }
                        if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                            $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                            $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                        }
                    }
                }
            } elseif (!empty($getdata['province'])) {
                $provinces = (new Region())->where(['area_code' => $getdata['province'], 'level' => 1])->column('area_code');
                foreach ($provinces as $k =>  $province) {
                    $data['result'][$k]['total'] = 0;
                    $data['result'][$k]['weight'] = 0;
                    $data['result'][$k]['todaytotal'] = 0;
                    $data['result'][$k]['todayweight'] = 0;
                    $orderson =  (new Order())->where(['status' => 3, 'province' => $province])->column('ordersn');
                    $todayorderson =  (new Order())->where(['status' => 3, 'province' => $province])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                    $data['result'][$k]['name'] = (new Region())->where(['area_code' => $province])->value('name');
                    $data['result'][$k]['order'] = count($orderson);
                    $data['result'][$k]['todayorder'] = count($todayorderson);
                    foreach ($orderlog as $orderlogs) {
                        if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                            $data['result'][$k]['total'] += $orderlogs['total'];
                            $data['result'][$k]['weight'] += $orderlogs['weight'];
                        }
                        if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                            $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                            $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                        }
                    }
                }
            }
        }
        if ($admin['type'] == 2) {
            $order = (new Order())->where(['status' => 3, 'area' => $admin['area']])->column('ordersn');
            $todayorder = (new Order())->where(['status' => 3, 'area' => $admin['area']])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
            foreach ($admin['area'] as $k =>  $area) {
                $data['result'][$k]['total'] = 0;
                $data['result'][$k]['weight'] = 0;
                $data['result'][$k]['todaytotal'] = 0;
                $data['result'][$k]['todayweight'] = 0;
                $orderson =  (new Order())->where(['status' => 3, 'area' => $area])->column('ordersn');
                $todayorderson =  (new Order())->where(['status' => 3, 'area' => $area])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                $data['result'][$k]['name'] = (new Region())->where(['area_code' => $area, 'level' => 3])->value('name');
                $data['result'][$k]['order'] = count($orderson);
                $data['result'][$k]['todayorder'] = count($todayorderson);
                foreach ($orderlog as $orderlogs) {
                    if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                        $data['result'][$k]['total'] += $orderlogs['total'];
                        $data['result'][$k]['weight'] += $orderlogs['weight'];
                    }
                    if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                        $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                        $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                    }
                }
            }
        }
        if ($admin['type'] == 3) {
            $order = (new Order())->where(['status' => 3, 'street' => $admin['street']])->column('ordersn');
            $todayorder = (new Order())->where(['status' => 3, 'street' => $admin['street']])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
            foreach ($admin['street'] as $k =>  $street) {
                $data['result'][$k]['total'] = 0;
                $data['result'][$k]['weight'] = 0;
                $data['result'][$k]['todaytotal'] = 0;
                $data['result'][$k]['todayweight'] = 0;
                $orderson =  (new Order())->where(['status' => 3, 'street' => $street])->column('ordersn');
                $todayorderson =  (new Order())->where(['status' => 3, 'street' => $street])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                $data['result'][$k]['name'] = (new Region())->where(['id' => $street])->value('name');
                $data['result'][$k]['order'] = count($orderson);
                $data['result'][$k]['todayorder'] = count($todayorderson);
                foreach ($orderlog as $orderlogs) {
                    if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                        $data['result'][$k]['total'] += $orderlogs['total'];
                        $data['result'][$k]['weight'] += $orderlogs['weight'];
                    }
                    if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                        $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                        $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                    }
                }
            }
        }
        if ($admin['type'] == 4) {
            $order = (new Order())->where(['status' => 3, 'street' => $admin['street']])->column('ordersn');
            $todayorder = (new Order())->where(['status' => 3, 'street' => $admin['street']])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
            foreach ($admin['street'] as $k =>  $street) {
                $data['result'][$k]['total'] = 0;
                $data['result'][$k]['weight'] = 0;
                $data['result'][$k]['todaytotal'] = 0;
                $data['result'][$k]['todayweight'] = 0;
                $orderson =  (new Order())->where(['status' => 3, 'street' => $street])->column('ordersn');
                $todayorderson =  (new Order())->where(['status' => 3, 'street' => $street])->whereBetweenTime('ordertime', $time, $endtime)->column('ordersn');
                $data['result'][$k]['name'] = (new Region())->where(['id' => $street])->value('name');
                $data['result'][$k]['order'] = count($orderson);
                $data['result'][$k]['todayorder'] = count($todayorderson);
                foreach ($orderlog as $orderlogs) {
                    if ($data['result'][$k]['order'] > 0 && in_array($orderlogs['ordersn'], $orderson)) {
                        $data['result'][$k]['total'] += $orderlogs['total'];
                        $data['result'][$k]['weight'] += $orderlogs['weight'];
                    }
                    if ($data['result'][$k]['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorderson)) {
                        $data['result'][$k]['todaytotal'] += $orderlogs['total'];
                        $data['result'][$k]['todayweight'] += $orderlogs['weight'];
                    }
                }
            }
        }
        $data['order'] = count($order);
        $data['todayorder'] = count($todayorder);
        $data['total'] = 0;
        $data['weight'] = 0;
        $data['todaytotal'] = 0;
        $data['todayweight'] = 0;
        $stime = microtime(true);
        foreach ($orderlog as $orderlogs) {
            if ($data['order'] > 0 && in_array($orderlogs['ordersn'], $order)) {
                $data['total'] += $orderlogs['total'];
                $data['weight'] += $orderlogs['weight'];
            }
            if ($data['todayorder'] > 0 && in_array($orderlogs['ordersn'], $todayorder)) {
                $data['todaytotal'] += $orderlogs['total'];
                $data['todayweight'] += $orderlogs['weight'];
            }
        }
        return view(null, [
            'admin' => $admin,
            'data' => $data,
            'time' => $time
        ]);
    }

    public function index()
    {
        $admin = $this->request->admin;
        $menu = config('menu.menu');
        $arr = [];
        foreach ($menu as $k => $v) {
            if ($v['show'] == true) {
                foreach ($v['sub'] as $x => $y) {
                    if (($y['show'] == true && in_array($y['uri'], $admin['role'])) || $admin['is_default'] == 1) {
                        $arr[$k]['name'] = $v['name'];
                        $arr[$k]['ico'] = $v['ico'];
                        $arr[$k]['sub'][$x] = $y;
                    }
                }
            }
        }
        if ($admin['type'] == 4 && !empty($arr['admin']['sub']['index'])) {
            unset($arr['admin']['sub']['index']);
        }
        return view(null, [
            'admin' => $admin,
            'menu' => $arr,
        ]);
    }


    public function miss()
    {
        return view('404');
    }

    public function admin()
    {
        $admin = Admin::get(session('admin_id'));
        if ($this->request->isPost()) {
            if (empty($admin)) {
                json(['status' => 1, 'msg' => '登陆已过期!']);
            }

            $data = input('post.');
            if ($data['password'] != '') {
                if ($data['password'] != $data['passworder']) {
                    return json(['status' => 1, 'info' => '密码不一致']);
                }
                $data1['password'] = md5(md5($data['password']) . md5($admin['entry']));
            }
            $data1['nickname'] = $data['nickname'];
            $data1['images'] = $data['images'];
            $data1['poster'] = $data['poster'];
            if ($admin->save($data1, ['id' => $admin->id])) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            return view('admin', ['admin' => $admin]);
        }
    }
}
